// yb_mingpian/pages/goods/search/search.js

var tget = getApp(),
  core = tget.requirejs("core"),
  e = tget.requirejs("check");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabStatus:0,
    page: 1,
    defaults: {
      keywords: "",
      isrecommand: "",
      ishot: "",
      isnew: "",
      cate_id: "",
      order: "",
      by: "desc", 
    },
    list:[]
  },
  checkTab:function(e){
    let that = this
    this.setData({
      tabStatus: e.target.dataset.id,
      defaults:{
        ...that.data.defaults,
        order:''
      },
      list:[],
      page:1
    })
    this.getList()
  },

  /**
   *搜索查询
   * @param  keywords  搜索关键字
   * @return array
   */
  bindSearch: function (t) {
    this.setData({
      list: [], //清空已存在商品列表
      loading: true,
    });
    var a = e.trim(t.detail.value),
      s = this.data.defaults;
    //console.log(a)
    "" != a ? (s.keywords = a, this.setData({
      page: 1,
      defaults: s,
      fromsearch: false
    }), this.getList()) : (s.keywords = "", this.setData({
      page: 1,
      defaults: s,
      fromsearch: false,
    }), this.getList())
  },
  /**
   * 动态获取焦点
   */
  inputing: function (t) {
    var inputing = e.trim(t.detail.value);
    this.setData({
      inputing: inputing
    })
  },
  /**
   * 放大镜获取input框中的value值
   */
  icon: function (e) {
    this.setData({
      list: [], //清空已存在商品列表
      loading: true,
    });
    var a = this.data.inputing,
      s = this.data.defaults;
    //console.log(a)
    "" != a ? (s.keywords = a, this.setData({
      page: 1,
      defaults: s,
      fromsearch: false
    }), this.getList()) : (s.keywords = "", this.setData({
      page: 1,
      defaults: s,
      fromsearch: false,
    }), this.getList())
  },
  /**
 *按综合、热销，价格排序
 */
  bindSort: function (t) {
    var a = core.pdata(t).order,
      e = this.data.defaults;
    // console.log(t)
    if ("" == a) {
      if (e.order == a)
        return;
      e.order = ""
    } else if ("price" == a) {
      e.order == a ? "desc" == e.by ? e.by = "asc" : e.by = "desc" : e.by = "asc", e.order = a
    } else if ("sales" == a) {
      if (e.order == a)
        return;
      e.order = "sales",
        e.by = "desc"
    }
    this.setData({
      defaults: e,
      page: 1,
      list: [],
      loading: true,

    }),
      // console.log(e)
      this.getList()
  },
  /**
   *获取商品列表
   * @param  keywords  搜索关键字
   * @param isrecommand 是否推荐
   * @param   ishot  是否热销
   * @param  isnew   是否新品
   * @param  cate_id  类型id
   * @param   order 排序字段
   * @param   by:  倒序/顺序
   * @return array
   */
  getList: function () {
    var t = this;
    t.setData({
      loading: true
    }),
      console.log("aa=======",t.data)
    t.data.defaults.page = t.data.page,
    t.data.defaults.uid = tget.getCache("userinfo").uid
    if (t.data.tabStatus == 0){ 
        core.get("goods/GoodsList", t.data.defaults, function (a) {
        console.log('商品',a)
          if (a.code == 0) {
            if (a.info.length == 0) {
              t.setData({
                loading: false
              })
              return false;
            }
            var e = {
              loading: false,
            };
            a.info.length > 0 && (e.page = t.data.page + 1, e.list = t.data.list.concat(a.info), a.info.length < 10 && (e.loaded = true)),
              t.setData(e)
          } else {
            core.alert(a.msg)
          }

        })
      }else{
      console.log("t.data.defaults.uid", t.data.defaults.uid)
        core.get("store/list", {
          keywords: t.data.defaults.keywords,
          order: t.data.defaults.order,
          by: t.data.defaults.by,
          uid: t.data.defaults.uid
        }, function (a) {
          console.log(a)
          if (a.code == 0) {
            if (a.info.length == 0) {
              t.setData({
                loading: false
              })
              return false;
            }
            var e = {
              loading: false,
            };
            a.info.length > 0 && (e.page = t.data.page + 1, e.list = t.data.list.concat(a.info), a.info.length < 10 && (e.loaded = true)),
              t.setData(e)
          } else {
            core.alert(a.msg)
          }

        })
      }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.key, '======', this.data)
    this.setData({
      defaults:{
        keywords: options.key
      }
    })
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      list: [],
      page: 1,
      loaded: false
    })
    this.getList()
    wx.stopPullDownRefresh();
  }, 

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.loaded || this.getList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})